package com.czn.controller.patient;

import com.czn.entity.*;
import com.czn.service.IAppointmentService;
import com.czn.service.IHospitalizationService;
import com.czn.service.IMedicalHistoryService;
import com.czn.service.IPatientService;
import com.czn.utils.PDFUtils;
import com.czn.vo.QueryVo;
import com.czn.vo.ResponseResult;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import javax.swing.filechooser.FileSystemView;
import java.util.List;
import java.util.Map;

/**
 * 患者端控制器
 *
 * @author ChenZeNan
 * @create 2021-04-25 22:18
 */
@Controller
@RequestMapping("/patient")
public class PatientController {

    @Autowired
    private IAppointmentService appointmentService;

    @Autowired
    private IPatientService patientService;

    @Autowired
    private IMedicalHistoryService medicalHistoryService;

    @Autowired
    private IHospitalizationService hospitalizationService;


    /**
     * 首页：显示患者所有预约记录
     *
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(QueryVo queryVo, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        queryVo.setUserId(user.getId());
        PageQuery<Appointment> page = appointmentService.findAppointmentList(queryVo);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", queryVo.getPageNo());
        model.addAttribute("path", "/patient/index");
        model.addAttribute("params", "");
        return "patient/appointmentHistory";
    }

    /**
     * 跳转到预约表
     *
     * @return
     */
    @RequestMapping(value = "/appointment", method = RequestMethod.GET)
    public String form(HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        Patient patient = patientService.findPatientByUserId(user.getId());
        model.addAttribute("patient", patient);
        return "patient/appointmentForm";
    }

    /**
     * 保存患者预约记录
     *
     * @param appointment
     * @return
     */
    @RequestMapping(value = "/appointment", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult save(@RequestBody Appointment appointment) {
        appointment.setStatus("1");
        Integer appointmentId = appointmentService.addAppointmentReturnId(appointment);
        Patient patient = new Patient();
        patient.setId(appointment.getPatientId());
        patient.setAppointmentId(appointmentId);
        patientService.editPatient(patient);
        return new ResponseResult("300", "预约成功！");
    }

    /**
     * 打印挂号单
     *
     * @param session
     * @return
     */
    @RequestMapping("/appointment/createPDF")
    @ResponseBody
    public ResponseResult createPDF(HttpSession session) {
        User user = (User) session.getAttribute("user");
        Patient patient = patientService.findPatientByUserId(user.getId());
        String s = null;
        try {
            Appointment appointment = appointmentService.findAppointmentById(patient.getAppointmentId());
            String path = FileSystemView.getFileSystemView().getHomeDirectory().getPath();
            s = PDFUtils.createAppointment(appointment, path+"\\");
            return new ResponseResult("600", s);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult("601", s);
        }
    }

    /**
     * 跳转到查询页面
     *
     * @return
     */
    @RequestMapping("/search")
    public String search() {
        return "patient/search";
    }

    /**
     * 根据类型查询信息
     *
     * @param type
     * @param name
     * @return
     */
    @RequestMapping("/searchInfo")
    @ResponseBody
    public Map<String, List> searchInfo(String type, String name) {
        Map<String, List> info = patientService.findInfo(type, name);
        return info;
    }

    /**
     * 跳转到病史页面
     *
     * @return
     */
    @RequestMapping("/medicalHistory")
    public String medicalHistory(QueryVo queryVo, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        queryVo.setUserId(user.getId());
        PageQuery<Medicalhistory> page = medicalHistoryService.findMedicalHistoryList(queryVo);
        model.addAttribute("page", page);
        model.addAttribute("pageNo", queryVo.getPageNo());
        model.addAttribute("path", "/patient/medicalHistory");
        model.addAttribute("params", "");
        return "patient/medicalHistory";
    }

    /**
     * 跳转到住院页面
     *
     * @return
     */
    @RequestMapping("/hospitalization")
    public String hospitalization(QueryVo queryVo, HttpSession session, Model model) {
        User user = (User) session.getAttribute("user");
        Hospitalization hospitalization = hospitalizationService.findOneById(user.getId());
        model.addAttribute("hospitalization", new Hospitalization());
        if (hospitalization != null) {
            model.addAttribute("hospitalization", hospitalization);
        }
        List<Hospitalization> list = hospitalizationService.findListById(user.getId());
        model.addAttribute("list", list);
        return "patient/hospitalization";
    }


}
